package drest.service;

import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
import java.util.Vector;
import jargs.gnu.CmdLineParser;
import jargs.gnu.CmdLineParser.Option;

public class ArgParser {
	private CmdLineParser parser;
	private Option show;
	private Option upload;
	private Option name;
	private Option data;
	private Option mode;
	private Option original_asset_name;
	private Option original_asset_path;
	private Option separator;
	private Option auto;
	private Option citation;
	private Option tags;
	
	public ArgParser() {
		this.parser = new CmdLineParser();
		this.show = this.parser.addBooleanOption('s', "show");
		this.upload = this.parser.addBooleanOption('u', "upload");
		this.name = this.parser.addStringOption('n', "name");
		this.data = this.parser.addStringOption('d', "data");
		this.mode = this.parser.addStringOption('m', "mode");
		this.original_asset_name = this.parser.addStringOption("original_asset_name");
		this.original_asset_path = this.parser.addStringOption("original_asset_path");
		this.separator = this.parser.addStringOption('p', "separator");
		this.auto = this.parser.addBooleanOption('a', "auto_estimate");
		this.citation = this.parser.addStringOption('c', "citation");
		this.tags = this.parser.addStringOption("tags");
	}
	
	
	
	public String getFile(String filename) {
		File datafile = new File(filename);
		FileReader fr;
		BufferedReader br;
		String line;
		String data = "";
		
		try {
			fr = new FileReader(datafile);
			br = new BufferedReader(fr);
			line = br.readLine();

			while (line != null) {
				data += line + "\n";
				line = br.readLine();
			}
			br.close();
			fr.close();
			
		} catch (Exception e) {
			System.err.println(e);
		}
		return data;
	}

	public CmdLineParser getParser()	{ return parser; }
	public Boolean getShow() 			{ return (Boolean)getParser().getOptionValue(this.show, Boolean.FALSE); }
	public Boolean getUpload() 			{ return (Boolean)getParser().getOptionValue(this.upload, Boolean.FALSE); }
	public String getName() 			{ return (String)getParser().getOptionValue(this.name); }
	public String getData() 			{ return (String)getParser().getOptionValue(this.data); }
	public String getMode() 			{ return (String)getParser().getOptionValue(this.mode); }
	public String getAssetName()		{ return (String)getParser().getOptionValue(this.original_asset_name); }
	public String getAssetPath()		{ return (String)getParser().getOptionValue(this.original_asset_path);	}
	public String getSeparator()		{ return (String)getParser().getOptionValue(this.separator); }
	public Boolean getAutoEst() 		{ return (Boolean)getParser().getOptionValue(this.auto, Boolean.TRUE); }
	public String getCitation() 		{ return (String)getParser().getOptionValue(this.citation); }
	public String getTags() 			{ return (String)getParser().getOptionValue(this.tags); }

	public Vector populate_upload_args() {
		Vector<Object> uargs = new Vector<Object>();
		uargs.addElement(getName());
		uargs.addElement(getData());
		uargs.addElement(getMode());
		uargs.addElement(getAssetName());
		uargs.addElement(getAssetPath());
		uargs.addElement(getSeparator());
		uargs.addElement(getCitation());
		return uargs;
	}
	
}